//从vue-router中按需导入两个方法
import { createRouter, createWebHashHistory } from 'vue-router'
 
 //导入路由页面（组件）
import Home from '../pages/home.vue'
import Login from '../pages/login.vue'
import ProdCategory from '../pages/prodCategory.vue'
import ProdList from '../pages/prodList.vue'
import Role from '../pages/role.vue'
import SysArea from '../pages/sysarea.vue'
import SysUser from '../pages/sysUser.vue'
// 创建路由实例对象
const router = createRouter({
  // 指定路由的工作模式
  history: createWebHashHistory(),
  // 自定义路由高亮的 class 类
  // 声明路由的匹配规则

  routes: [
	  {path:'/',component:Login},
	  {path:'/home',component:Home,   //#/home/prodList
		children:[
			  {path:'prodlist',component:ProdList},  //注多级路由的path不能加/
			  {path:'prodcategory',component:ProdCategory},
			  {path:'role',component:Role},
			   {path:'sysarea',component:SysArea},
			    {path:'sysuser',component:SysUser},
		],
	   },
	  {path:'/login',component:Login},
	
  ],
})

import {useloginUserStore} from '../store/loginUserInfo.js'
import { ElMessage } from 'element-plus'

router.beforeEach((to,from,next)=>{
	let userInfoStore = useloginUserStore();
	if (to.path.indexOf("/home/")!=-1){
		//判断用户是否登录，未登录则跳转到登录页面
		if(userInfoStore.user_id){
			 //放行 //已登录的状态下可以放行
			let nextFlag = false;
			for(let i=0;i<userInfoStore.urlList.length;i++){
				if (to.path==userInfoStore.urlList[i]){
					nextFlag= true;
					break;
				}
			}
			if (nextFlag){
				next();
			}else{
				ElMessage({type:"error",message:"您没有访问权限"})
				router.replace(from.path)
			}
		}else{
			ElMessage({type:"error",message:"您还未登录请先登录"})
			router.replace("/login")
		}
	}else{
			next(); //放行 //访问非 /home开头的组件时，可以访问
	}
	console.log("from:",from);
	console.log("next",next);

})


// 导出路由对象
export default router